package com.hencoder.hencoderpracticedraw1.practice;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import static android.animation.ValueAnimator.RESTART;

public class Practice10HistogramView extends View {
    private Paint mPaint;
    private Path mPath;
    private int mWidth;
    private int mHeight;
    private int countX =7;
    private int countY =7;
    private int unitXLength;
    private int unitYLength;
    //住形图宽度dp
    private int cloumnWidth=20;
    //坐标原点位置
    //坐标原点位置
    private final int originX = dip2px( 20);
    private final int originY = dip2px( 20);
    private int xTitleSize=12;
    private float animationPercent=1;
    //柱状图数据颜色
    private int[] columnColors = new int[]{Color.parseColor("#fa7b48"),Color.parseColor("#fca640"),Color.parseColor("#f5bb47"),
            Color.parseColor("#99b943"),Color.parseColor("#63c3db"),Color.parseColor("#649cdb"),Color.parseColor("#619cdb")};
    private String[] titles={"January","February","March","April","May","June","July","August","January"};
    private Integer[] datas={30,42,34,12,43,56,23};
    public Practice10HistogramView(Context context) {
        super(context);
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint=new Paint();
        mPath=new Path();
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth=getDisplayMetrics().widthPixels- originX*3/2;
        mHeight=getMeasuredHeight()-originY*3/2;
    }
    private DisplayMetrics getDisplayMetrics(){
        return getContext().getResources().getDisplayMetrics();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawAxisX(canvas,mPaint);
//画y轴
        drawAxisY(canvas,mPaint);
        //x刻度线
        drawXScale(canvas,mPaint);
        drawYScale(canvas,mPaint);
        //画数据
        drawColumn(canvas,mPaint);
//        x轴标题
        drawxTitle(canvas,mPaint);
//        /
        drawyTitle(canvas,mPaint);
    }

    int dip2px(float dipValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 绘制横坐标轴（X轴）
     *
     * @param canvas
     * @param paint
     */
    private void drawAxisX(Canvas canvas, Paint paint) {
        paint.setColor(Color.parseColor("#c0c0c0"));
        //设置画笔宽度
        paint.setStrokeWidth(3);
        //设置画笔抗锯齿
        paint.setAntiAlias(true);
        //画横轴(X)
        canvas.drawLine(originX, mHeight, originX + mWidth, mHeight, paint);

    }

    /**
     * 绘制横标轴（Y轴）
     * @param canvas
     * @param paint
     */
    private void drawAxisY(Canvas canvas,Paint paint){
        paint.setColor(Color.parseColor("#c0c0c0"));
        //设置画笔宽度
        paint.setStrokeWidth(3);
        //设置画笔抗锯齿
        paint.setAntiAlias(true);
        //画轴(Y)
        canvas.drawLine(originX, mHeight, originX ,originY, paint);
    }
    /**
     * x轴画刻度
     */
    private void drawXScale(Canvas canvas, Paint paint){
        paint.setColor(Color.parseColor("#c0c0c0"));
        //设置画笔宽度
        paint.setStrokeWidth(3);
        //设置画笔抗锯齿
        paint.setAntiAlias(true);
        unitXLength=(mWidth-dip2px(30))/ countX;
        int tempX=unitXLength+originX;
        for (int i = 0; i < countX; i++) {
            canvas.drawLine(tempX,mHeight,tempX,mHeight+dip2px(3),paint);
            tempX+=unitXLength;
        }
    }

    /**
     * y轴画刻度
     */
    private void drawYScale(Canvas canvas, Paint paint){
        paint.setColor(Color.parseColor("#c0c0c0"));
        //设置画笔宽度
        paint.setStrokeWidth(3);
        //设置画笔抗锯齿
        paint.setAntiAlias(true);
        unitYLength=(mHeight-dip2px(20))/ countY;
        int tempY=mHeight-originY;
        for (int i = 0; i < countY; i++) {
            canvas.drawLine(originX,tempY,originX-dip2px(3),tempY,paint);
            tempY=tempY-unitYLength;
        }
    }
    /**
     * 画柱形图
     */
    private void drawColumn(Canvas canvas,Paint Paint){
        Paint.setStyle(android.graphics.Paint.Style.FILL);
        int startx=originX;
        int drawx,enddrawx;
        int drawy=mHeight,enddrawy;
        for (int i = 0; i <countX ; i++) {
            int data=datas[i];
            Paint.setColor(columnColors[i]);
            int endx=startx+unitXLength;
            int middle=(startx+endx)/2;
            drawx=middle-dip2px(cloumnWidth)/2;
            enddrawx=middle+dip2px(cloumnWidth)/2;
            float percent=data/10.f;
            enddrawy= (int) ((mHeight-percent*unitYLength));
            canvas.drawRect(drawx,enddrawy,enddrawx,mHeight,mPaint);
            startx+=unitXLength;
        }
    }
    /**
     *x轴的标题
     */
    private void drawxTitle(Canvas canvas,Paint paint){
        paint.setStyle(android.graphics.Paint.Style.FILL);
        paint.setColor(Color.parseColor("#c0c0c0"));
        int startx=originX;
        int drawx,enddrawx;
        int drawy=mHeight+dip2px(20),enddrawy = 0;
        for (int i = 0; i <countX ; i++) {
            int endx=startx+unitXLength;
            int middle=(startx+endx)/2;
            //计算字的大小
            paint.setTextSize(dip2px(12));
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText(titles[i],middle,drawy,paint);
//            canvas.drawText("1",drawx,drawy,enddrawx,enddrawy,paint);
            startx+=unitXLength;
        }
    }
    /**
     *y轴的标题
     */
    private void drawyTitle(Canvas canvas,Paint paint){
        int starty=mHeight-originY;
        for (int i = 0; i <countY ; i++) {
            paint.setTextSize(dip2px(12));
            paint.setTextAlign(Paint.Align.RIGHT);
            canvas.drawText((i+1)*10+"",originX-dip2px(5),starty+dip2px(5),paint);
            starty-=unitYLength;
        }
    }


}
